<?php

namespace App\Http\Controllers\Api\V1;

use App\B2BProject\Site\Article;
use App\B2BProject\Site\ArticleCategory;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($type = $request->query('type')) {
            if ($type == 1) {
                $articles = new Article();
            } else {
                $filter   = ArticleCategory::categoryFilter();
                $articles = Article::WhereIn('category_id', $filter);
            }
        } else {
            $filter   = ArticleCategory::categoryFilter();
            $articles = Article::whereNotIn('category_id', $filter);
        }

        if ($cover = $request->query('cover')) {
            $articles->collectionWithCover();
        }

        $articles = $articles->display()->orderBy('sort', 'desc')->orderBy('created_at', 'desc')->get();

        $data = [];

        foreach ($articles as $article) {
            $data[] = [
                'id'                => $article->id,
                'title'             => $article->title,
                'href'              => $article->href,
                'cover'             => url($article->cover),
                'subject'           => mb_substr(trim(strip_tags($article->content)), 0, 127, 'UTF-8'),
                'publish_time'      => date('Y-m-d H:i:s', strtotime($article->created_at)),
                'publish_timestamp' => strtotime($article->created_at),
                'category_id'       => $article->category_id,
                'category'          => $article->category->display_name,
                'category_name'     => $article->category->name,
                'author_id'         => $article->admin_id,
                'author'            => $article->author->user,
            ];
        }

        return success_return(['list' => $data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $article = Article::findOrFail($id);

        return success_return([
            'id'                => $article->id,
            'title'             => $article->title,
            'href'              => $article->href,
            'cover'             => url($article->cover),
            'subject'           => mb_substr(trim(strip_tags($article->content)), 0, 127, 'UTF-8'),
            'content'           => preg_replace_callback('#src="((/[-0-9a-zA-Z]+)+\.[0-9a-zA-Z]+)"#ui', function ($matches) {
                return 'src="' . url($matches[1]) . '"';
            }, $article->content),
            'publish_time'      => date('Y-m-d H:i:s', strtotime($article->created_at)),
            'publish_timestamp' => strtotime($article->created_at),
            'category_id'       => $article->category_id,
            'category'          => $article->category->display_name,
            'category_name'     => $article->category->name,
            'author_id'         => $article->admin_id,
            'author'            => $article->author->user,
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int                      $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
